<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>008.C++基础入门 - 结构体 | 扣小妖</title><meta name="author" content="扣小妖"><meta name="copyright" content="扣小妖"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="结构体基本概念结构体属于用户自定义的数据类型，允许用户存储不同的数据类型。  结构体定义和使用在线课程 在线课程 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1et411b73Z?p&#x3D;64    课程视频下载到本地学习       语法：   struct 结构体名 &amp;#123; 结构体成员列表 &amp;#125;; 通过结构体创建变量的方式有三种：  struct 结构体名 变量">
<meta property="og:type" content="article">
<meta property="og:title" content="008.C++基础入门 - 结构体">
<meta property="og:url" content="http://liukuaizhuan.gitee.io/posts/4934793.html">
<meta property="og:site_name" content="扣小妖">
<meta property="og:description" content="结构体基本概念结构体属于用户自定义的数据类型，允许用户存储不同的数据类型。  结构体定义和使用在线课程 在线课程 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1et411b73Z?p&#x3D;64    课程视频下载到本地学习       语法：   struct 结构体名 &amp;#123; 结构体成员列表 &amp;#125;; 通过结构体创建变量的方式有三种：  struct 结构体名 变量">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/fb32dd64e26579054d2252368f88c81c.png">
<meta property="article:published_time" content="2023-02-03T11:54:56.000Z">
<meta property="article:modified_time" content="2023-02-04T09:58:20.451Z">
<meta property="article:author" content="扣小妖">
<meta property="article:tag" content="C++基础入门">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/fb32dd64e26579054d2252368f88c81c.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://liukuaizhuan.gitee.io/posts/4934793"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '008.C++基础入门 - 结构体',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-04 17:58:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/fb32dd64e26579054d2252368f88c81c.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">扣小妖</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">008.C++基础入门 - 结构体</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2023-02-03T11:54:56.000Z" title="发表于 2023-02-03 19:54:56">2023-02-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/C-%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8/">C++基础入门</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="008.C++基础入门 - 结构体"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="结构体基本概念"><a href="#结构体基本概念" class="headerlink" title="结构体基本概念"></a>结构体基本概念</h2><p>结构体属于用户自定义的数据类型，允许用户存储不同的数据类型。</p>
<hr>
<h2 id="结构体定义和使用"><a href="#结构体定义和使用" class="headerlink" title="结构体定义和使用"></a>结构体定义和使用</h2><details class="toggle" ><summary class="toggle-button" style="">在线课程</summary><div class="toggle-content"><ul>
<li><p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=64">https://www.bilibili.com/video/BV1et411b73Z?p=64</a>  </p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://sharewh1.xuexi365.com/share/download/a683a5eff285a8dbb332cdd57e3026ec">课程视频下载到本地学习</a> </p>
</li>
</ul>
</div></details> 

<p><strong>语法：</strong>   <code>struct 结构体名 &#123; 结构体成员列表 &#125;;</code></p>
<p>通过结构体创建变量的方式有三种：</p>
<ul>
<li>struct 结构体名 变量名 <mark class="hl-label red">☆常用☆</mark> </li>
<li>struct 结构体名 变量名={成员1值，成员2值…..} <mark class="hl-label red">☆常用☆</mark> </li>
<li>定义结构体时顺便创建变量 <mark class="hl-label blue">不推荐</mark> </li>
</ul>
<style>
   div .img_left{
        float:left;
    }
</style>

<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac on 2023/2/3.</span></span><br><span class="line"><span class="comment">// 结构体的定义和使用</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 1.创建学生数据类型，学生包括姓名，年龄，分数</span></span><br><span class="line"><span class="comment">// 自定义数据类型，一些类型集合组成的一个类型</span></span><br><span class="line"><span class="comment">// 语法 struct 类型名称&#123; 成员列表 &#125;</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="comment">// 成员列表</span></span><br><span class="line">    string name; <span class="comment">//姓名</span></span><br><span class="line">    <span class="type">int</span> age;      <span class="comment">//年龄</span></span><br><span class="line">    <span class="type">int</span> score;    <span class="comment">//分数</span></span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 2.通过学生类型创建具体学生</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 创建方法 3种</span></span><br><span class="line">    <span class="comment">// 2.1 struct Student s1</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student</span> s1;</span><br><span class="line">    <span class="comment">// 结构体在创建变量的时候，struct关键字可以省略 ,但是在定义的时候struct关键字是不可以省略的</span></span><br><span class="line">    Student s_test;</span><br><span class="line">    <span class="comment">// 给s1属性赋值 ,通过.访问结构体变量中的属性</span></span><br><span class="line">    s1.name = <span class="string">&quot;张飒&quot;</span>;</span><br><span class="line">    s1.age = <span class="number">15</span>;</span><br><span class="line">    s1.score = <span class="number">150</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;学生：&quot;</span> &lt;&lt; s1.name &lt;&lt; <span class="string">&quot; \t年龄：&quot;</span> &lt;&lt; s1.age &lt;&lt; <span class="string">&quot;\t 成绩：&quot;</span> &lt;&lt; s1.score &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.2 struct Student s2 = &#123;...&#125;</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student</span> s2 = &#123;<span class="string">&quot;李四&quot;</span>, <span class="number">19</span>, <span class="number">80</span>&#125;;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;学生：&quot;</span> &lt;&lt; s2.name &lt;&lt; <span class="string">&quot; \t年龄：&quot;</span> &lt;&lt; s2.age &lt;&lt; <span class="string">&quot;\t 成绩：&quot;</span> &lt;&lt; s2.score &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.3 在定义结构体时候顺便创建结构体变量 【不太推荐此操作，但是需要了解】</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student_chuzhong</span> &#123;</span><br><span class="line">        <span class="comment">// 成员列表</span></span><br><span class="line">        string name; <span class="comment">//姓名</span></span><br><span class="line">        <span class="type">int</span> age;      <span class="comment">//年龄</span></span><br><span class="line">        <span class="type">int</span> score;    <span class="comment">//分数</span></span><br><span class="line"></span><br><span class="line">    &#125; s3;</span><br><span class="line"></span><br><span class="line">    s3.name = <span class="string">&quot;王五&quot;</span>;</span><br><span class="line">    s3.age = <span class="number">20</span>;</span><br><span class="line">    s3.score = <span class="number">500</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;学生：&quot;</span> &lt;&lt; s3.name &lt;&lt; <span class="string">&quot; \t年龄：&quot;</span> &lt;&lt; s3.age &lt;&lt; <span class="string">&quot;\t 成绩：&quot;</span> &lt;&lt; s3.score &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3种创建方式，用的比较多的是第一种和第二种 ，第三种很少使用的话，是因为顺便创建的变量很容易忽略</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<mark class="hl-label red">总结：</mark> 

<ul>
<li>总结1：定义结构体时的关键字是struct，不可省略</li>
<li>总结2：创建结构体变量时，关键字struct可以省略</li>
<li>总结3：结构体变量利用操作符 <code>.</code> 访问成员</li>
</ul>
<hr>
<h2 id="结构体数组"><a href="#结构体数组" class="headerlink" title="结构体数组"></a>结构体数组</h2><details class="toggle" ><summary class="toggle-button" style="">在线课程</summary><div class="toggle-content"><ul>
<li><p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=65">https://www.bilibili.com/video/BV1et411b73Z?p=65</a>  </p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://sharewh1.xuexi365.com/share/download/3c20351d4905d452931dc43378460df9">课程视频下载到本地学习</a>  </p>
</li>
</ul>
</div></details> 

<p><strong>作用：</strong> 将自定义的结构体放入到数组中方便维护</p>
<p><strong>语法：</strong> <code>struct 结构体名 数组名[元素个数] = &#123; &#123;&#125;,&#123;&#125;,......&#123;&#125;  &#125;;</code></p>
<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Created by Mac on 2023/2/3.</span></span><br><span class="line"><span class="comment">// 结构体数组</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//结构体数组</span></span><br><span class="line">    <span class="comment">//1.定义结构体</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">        string name;</span><br><span class="line">        <span class="type">int</span> age;</span><br><span class="line">        <span class="type">int</span> score;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//2.创建结构体数组</span></span><br><span class="line">    Student stuArr[] = &#123;</span><br><span class="line">            &#123;<span class="string">&quot;张三&quot;</span>, <span class="number">13</span>, <span class="number">100</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;李四&quot;</span>, <span class="number">14</span>, <span class="number">150</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;王五&quot;</span>, <span class="number">16</span>, <span class="number">180</span>&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//3.给结构体数组中的元素赋值</span></span><br><span class="line">    stuArr[<span class="number">2</span>].name = <span class="string">&quot;赵六&quot;</span>;</span><br><span class="line">    stuArr[<span class="number">2</span>].age = <span class="number">80</span>;</span><br><span class="line">    stuArr[<span class="number">2</span>].score = <span class="number">50</span>;</span><br><span class="line">    <span class="comment">//4.遍历结构体数组</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;学生姓名：&quot;</span> &lt;&lt; stuArr[i].name &lt;&lt; <span class="string">&quot;\t 年龄：&quot;</span> &lt;&lt; stuArr[i].age &lt;&lt; <span class="string">&quot;\t成绩：&quot;</span> &lt;&lt; stuArr[i].score &lt;&lt; endl;</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<hr>
<h2 id="结构体指针"><a href="#结构体指针" class="headerlink" title="结构体指针"></a>结构体指针</h2><details class="toggle" ><summary class="toggle-button" style="">在线课程：结构体指针</summary><div class="toggle-content"><ul>
<li><p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=66">https://www.bilibili.com/video/BV1et411b73Z?p=66</a> </p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://sharewh1.xuexi365.com/share/download/8ad1e58138ef445a9dfcadbdc60f90fa">课程视频下载到本地学习</a> </p>
</li>
</ul>
</div></details>

<p><strong>作用：</strong> 通过指针访问结构体中的成员</p>
<ul>
<li>利用操作符 <code>-&gt;</code> 可以通过结构体指针访问结构体属性</li>
</ul>
<p>示例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="comment">// 结构体指针</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 定义结构体指针</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    string name;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    <span class="type">int</span> score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 1.创建学生结构体变量</span></span><br><span class="line">    Student s = &#123;<span class="string">&quot;张三&quot;</span>, <span class="number">10</span>, <span class="number">120</span>&#125;;</span><br><span class="line">    <span class="comment">// 2.通过指针指向结构体变量</span></span><br><span class="line">    Student *p = &amp;s;</span><br><span class="line">    <span class="comment">//3.通过指针访问结构体变量中的数据</span></span><br><span class="line">    <span class="comment">// 通过结构体指针 访问结构体中的属性，需要利用 -&gt; 符号</span></span><br><span class="line">    cout &lt;&lt; p-&gt;name &lt;&lt; <span class="string">&quot;\t年龄：&quot;</span> &lt;&lt; p-&gt;age &lt;&lt; <span class="string">&quot;\t分数：&quot;</span> &lt;&lt; p-&gt;score &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<mark class="hl-label red">总结：结构体指针可以通过->操作符来访问结构体中的成员</mark> 

<hr>
<h2 id="结构体嵌套结构体"><a href="#结构体嵌套结构体" class="headerlink" title="结构体嵌套结构体"></a>结构体嵌套结构体</h2><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=67">https://www.bilibili.com/video/BV1et411b73Z?p=67</a></p>
</blockquote>
<p><strong>作用：</strong> 结构体中的成员可以是另一个结构体</p>
<p><strong>例如：</strong> 每个老师辅导一个学员，一个老师的结构中，记录一个学生的结构体</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="comment">// 结构体嵌套结构体 </span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">//定义学生结构体</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    string name;        <span class="comment">/* 学生姓名 */</span></span><br><span class="line">    <span class="type">int</span> age;            <span class="comment">/* 学生年龄 */</span></span><br><span class="line">    <span class="type">int</span> score;          <span class="comment">/* 学生成绩 */</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义老师结构体</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Teacher</span> &#123;</span><br><span class="line">    <span class="type">int</span> id;             <span class="comment">/* 教师编号 */</span></span><br><span class="line">    string name;        <span class="comment">/* 教师姓名 */</span></span><br><span class="line">    <span class="type">int</span> age;            <span class="comment">/* 教师年龄 */</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student</span> stu; <span class="comment">/* 所教学生 */</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 结构体嵌入结构体</span></span><br><span class="line">    <span class="comment">// 创建老师</span></span><br><span class="line">    Teacher t;</span><br><span class="line">    t.id = <span class="number">1000</span>;</span><br><span class="line">    t.name = <span class="string">&quot;老王&quot;</span>;</span><br><span class="line">    t.age = <span class="number">50</span>;</span><br><span class="line">    t.stu.name = <span class="string">&quot;小王&quot;</span>;</span><br><span class="line">    t.stu.age = <span class="number">10</span>;</span><br><span class="line">    t.stu.score = <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;老师姓名：&quot;</span> &lt;&lt; t.name &lt;&lt; <span class="string">&quot;,\t编号：&quot;</span> &lt;&lt; t.id &lt;&lt; <span class="string">&quot;,\t年龄：&quot;</span> &lt;&lt; t.age &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;所教学生信息:&quot;</span>&lt;&lt;endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;学生姓名：&quot;</span> &lt;&lt; t.stu.name &lt;&lt; <span class="string">&quot;\t学生年龄：&quot;</span> &lt;&lt; t.stu.age &lt;&lt; <span class="string">&quot;\t学生成绩：&quot;</span> &lt;&lt; t.stu.score</span><br><span class="line">         &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<hr>
<h2 id="结构体做函数参数"><a href="#结构体做函数参数" class="headerlink" title="结构体做函数参数"></a>结构体做函数参数</h2><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=68">https://www.bilibili.com/video/BV1et411b73Z?p=68</a></p>
</blockquote>
<p><strong>作用：</strong> 将结构体作为参数向函数中传递</p>
<p>传递方式有两种：</p>
<ul>
<li>值传递</li>
<li>地址传递</li>
</ul>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="comment">// 结构体做函数参数</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    string name;    <span class="comment">/* 学生姓名 */</span></span><br><span class="line">    <span class="type">int</span> age;        <span class="comment">/* 学生年龄 */</span></span><br><span class="line">    <span class="type">int</span> score;      <span class="comment">/* 学生成绩 */</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印学生信息</span></span><br><span class="line"><span class="comment">// 1.值传递</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">print_student_type1</span><span class="params">(<span class="keyword">struct</span> Student s)</span> </span>&#123;</span><br><span class="line">    s.age = <span class="number">500</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;子函数1(值传递)中 \t 姓名：&quot;</span> &lt;&lt; s.name &lt;&lt; <span class="string">&quot;\t年龄：&quot;</span> &lt;&lt; s.age &lt;&lt; <span class="string">&quot;\t成绩：&quot;</span> &lt;&lt; s.score &lt;&lt; endl;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.地址传递</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">print_student_type2</span><span class="params">(<span class="keyword">struct</span> Student * p)</span></span>&#123;</span><br><span class="line">    p-&gt;age = <span class="number">100</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;子函数2(地址传递)中 \t 姓名：&quot;</span> &lt;&lt; p-&gt;name &lt;&lt; <span class="string">&quot;\t年龄：&quot;</span> &lt;&lt; p-&gt;age &lt;&lt; <span class="string">&quot;\t成绩：&quot;</span> &lt;&lt; p-&gt;score &lt;&lt; endl;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 结构体做函数参数</span></span><br><span class="line">    <span class="comment">// 将学生传入到一个参数中，打印学生身上的所有信息</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建结构体变量</span></span><br><span class="line">    Student s;</span><br><span class="line">    s.name = <span class="string">&quot;张三&quot;</span>;</span><br><span class="line">    s.age = <span class="number">20</span>;</span><br><span class="line">    s.score = <span class="number">150</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;main函数中打印【初始的】结果： 姓名&quot;</span> &lt;&lt;s.name &lt;&lt;<span class="string">&quot; 年龄：&quot;</span>&lt;&lt;s.age&lt;&lt;<span class="string">&quot; 分数： &quot;</span>&lt;&lt; s.score&lt;&lt;endl;</span><br><span class="line">    <span class="built_in">print_student_type1</span>(s);</span><br><span class="line">    <span class="built_in">print_student_type2</span>(&amp;s);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;main函数中打印【改变后】的结果： 姓名&quot;</span> &lt;&lt;s.name &lt;&lt;<span class="string">&quot; 年龄：&quot;</span>&lt;&lt;s.age&lt;&lt;<span class="string">&quot; 分数： &quot;</span>&lt;&lt; s.score&lt;&lt;endl;</span><br><span class="line">     </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<mark class="hl-label red">总结：如果不想改变主函数中的数据，用值传递，反之用地址传递。</mark> 

<hr>
<h2 id="结构体中const使用场景"><a href="#结构体中const使用场景" class="headerlink" title="结构体中const使用场景"></a>结构体中const使用场景</h2><p><strong>作用：</strong>  用const来防止误操作</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// const 使用场景： 防止误操作</span></span><br><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    string name;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    <span class="type">int</span> score;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 将函数中的形参改为指针，可以减少内存空间，而且不会复制新的副本</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">print_Student_Info</span><span class="params">(<span class="type">const</span> Student *s)</span> </span>&#123;  <span class="comment">// const 常量指针，指向可以改，指向的值不可以改</span></span><br><span class="line">    <span class="comment">// 使用值传递， 复制信息，参数越多，占用的内存会越多</span></span><br><span class="line">    <span class="comment">//cout &lt;&lt; &quot;姓名：&quot; &lt;&lt; s.name &lt;&lt; &quot;\t年龄:&quot; &lt;&lt; s.age &lt;&lt; &quot;\t 得分：&quot; &lt;&lt; s.score &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">// 地址传递，传递的是地址 才4个字节，指针访问属性，是使用-&gt; 箭头访问的</span></span><br><span class="line">   <span class="comment">// s-&gt;age = 15;  //加入const后，一旦有修改的操作就会报错，可以防止我们的误操作</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;姓名：&quot;</span> &lt;&lt; s-&gt;name &lt;&lt; <span class="string">&quot;\t年龄:&quot;</span> &lt;&lt; s-&gt;age &lt;&lt; <span class="string">&quot;\t 得分：&quot;</span> &lt;&lt; s-&gt;score &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// const 的使用场景</span></span><br><span class="line">    <span class="comment">// 创建结构体变量</span></span><br><span class="line">    Student s = &#123;<span class="string">&quot;张三&quot;</span>, <span class="number">100</span>, <span class="number">150</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 通过函数打印结构体变量信息</span></span><br><span class="line">    <span class="built_in">print_Student_Info</span>(&amp;s);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;在main函数中\t姓名:&quot;</span> &lt;&lt; s.name &lt;&lt; <span class="string">&quot;\t年龄:&quot;</span> &lt;&lt; s.age &lt;&lt; <span class="string">&quot;\t 得分：&quot;</span> &lt;&lt; s.score &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<hr>
<h2 id="结构体案例1"><a href="#结构体案例1" class="headerlink" title="结构体案例1"></a>结构体案例1</h2><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=70">https://www.bilibili.com/video/BV1et411b73Z?p=70</a> </p>
</blockquote>
<p><strong>案例1描述:</strong></p>
<p>学校正在做毕业设计项目，每名老师带领5个学生，总共有3名老师，需求如下：<br>设计学生和老师的结构体，其中在老师的结构体中，有老师姓名和一个存放5名学生的数组作为成员。<br>学生的成员有姓名，考试分数，创建数组存放3名老师，通过函数给每个老师及所带的学生赋值<br>最终打印出老师数据以及老师所带的学生数据。</p>
<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="comment">// 案例1</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;ctime&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 案例需求：</span></span><br><span class="line"><span class="comment"> * 学校正在做毕业设计项目，每名老师带领5个学生，总共有3名老师，需求如下：</span></span><br><span class="line"><span class="comment"> * 设计学生和老师的结构体，其中在老师的结构体中，有老师姓名和一个存放5名学生的数组作为成员。</span></span><br><span class="line"><span class="comment"> * 学生的成员有姓名，考试分数，创建数组存放3名老师，通过函数给每个老师及所带的学生赋值</span></span><br><span class="line"><span class="comment"> * 最终打印出老师数据以及老师所带的学生数据。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 学生的结构体定义 */</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    string sName; <span class="comment">/* 学生姓名 */</span></span><br><span class="line">    <span class="type">int</span> score;   <span class="comment">/* 学生分数 */</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 老师的结构体定义 */</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Tearcher</span> &#123;</span><br><span class="line">    string tName; <span class="comment">/* 老师姓名 */</span></span><br><span class="line">    <span class="keyword">struct</span> <span class="title class_">Student</span> sArray[<span class="number">5</span>];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 给老师和学生赋值的函数  */</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">allCateSpace</span><span class="params">(Tearcher tArray[], <span class="type">int</span> length)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 给老师开始赋值</span></span><br><span class="line">    string nameSeed = <span class="string">&quot;ABCDE&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; length; i++) &#123;</span><br><span class="line">        tArray[i].tName = <span class="string">&quot;Teacher_&quot;</span>;</span><br><span class="line">        tArray[i].tName += nameSeed[i];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过循环给每名老师所带的学生赋值</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; <span class="number">5</span>; j++) &#123;</span><br><span class="line">            tArray[i].sArray[j].sName = <span class="string">&quot;Student_&quot;</span>;</span><br><span class="line">            tArray[i].sArray[j].sName += nameSeed[j]; <span class="comment">// 学生姓名</span></span><br><span class="line">            <span class="comment">// 随机分数</span></span><br><span class="line">            <span class="type">int</span> random = <span class="built_in">rand</span>() % <span class="number">61</span> + <span class="number">40</span>;<span class="comment">// 随机 0-59分的数 + 40 则变为 随机40-99</span></span><br><span class="line">            tArray[i].sArray[j].score = random; <span class="comment">// 学生分数</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印所有信息</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printInfo</span><span class="params">(Tearcher tArray[], <span class="type">int</span> len)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;老师的姓名：&quot;</span> &lt;&lt; tArray[i].tName &lt;&lt; <span class="string">&quot;:&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; <span class="number">5</span>; j++) &#123;</span><br><span class="line">            cout &lt;&lt; <span class="string">&quot;\t学生：&quot;</span> &lt;&lt; tArray[i].sArray[j].sName &lt;&lt; <span class="string">&quot;\t&quot;</span></span><br><span class="line">                 &lt;&lt; <span class="string">&quot;分数：&quot;</span> &lt;&lt; tArray[i].sArray[j].score &lt;&lt; endl;</span><br><span class="line">        &#125;</span><br><span class="line">        cout &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 随机数种子</span></span><br><span class="line">    <span class="built_in">srand</span>((<span class="type">unsigned</span> <span class="type">int</span>) <span class="built_in">time</span>(<span class="literal">NULL</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.创建3名老师的数组</span></span><br><span class="line">    Tearcher tArray[<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.通过函数给3名老师的信息赋值，并给老师带的学生信息赋值</span></span><br><span class="line">    <span class="type">int</span> length = <span class="built_in">sizeof</span>(tArray) / <span class="built_in">sizeof</span>(tArray[<span class="number">0</span>]); <span class="comment">// 获取数组长度</span></span><br><span class="line">    <span class="built_in">allCateSpace</span>(tArray, length);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3.打印所有老师及所带的学生信息</span></span><br><span class="line">    <span class="built_in">printInfo</span>(tArray, length);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="结构体案例2"><a href="#结构体案例2" class="headerlink" title="结构体案例2"></a>结构体案例2</h2><blockquote>
<p>在线课程 <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=72">https://www.bilibili.com/video/BV1et411b73Z?p=72</a></p>
</blockquote>
<p><strong>案例描述：</strong></p>
<p>设计一个英雄的结构体，包括成员姓名，年龄，性别；创建结构体数组，数组中存放5名英雄。<br>通过冒泡排序的算法，将数组中的英雄按照年龄进行升序排序，最终打印排序后的结果。</p>
<p>五名英雄信息如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="string">&quot;刘备&quot;</span>,<span class="number">23</span>,<span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">&#123;<span class="string">&quot;关于&quot;</span>,<span class="number">22</span>,<span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">&#123;<span class="string">&quot;张飞&quot;</span>,<span class="number">20</span>,<span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">&#123;<span class="string">&quot;赵云&quot;</span>,<span class="number">21</span>,<span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">&#123;<span class="string">&quot;貂蝉&quot;</span>,<span class="number">19</span>,<span class="string">&quot;女&quot;</span>&#125;,</span><br></pre></td></tr></table></figure>

<p><strong>示例：</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Created by Mac on 2023/2/4.</span></span><br><span class="line"><span class="comment">// 案例</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;iostream&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;string&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">//1.设计英雄结构体</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Hero</span> &#123;</span><br><span class="line">    string name;    <span class="comment">// 姓名</span></span><br><span class="line">    <span class="type">int</span> age;        <span class="comment">// 年龄</span></span><br><span class="line">    string sex;     <span class="comment">// 性别</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 冒泡排序，升序</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">paixu_Persion</span><span class="params">(Hero heroArray[], <span class="type">int</span> len)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len - <span class="number">1</span>; ++i) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; len - i - <span class="number">1</span>; ++j) &#123;</span><br><span class="line">            <span class="comment">// 对年龄进行升序排序 如果j下标年龄大于j+1的元素年龄，交换两个元素</span></span><br><span class="line">            <span class="keyword">if</span> (heroArray[j].age &gt; heroArray[j + <span class="number">1</span>].age) &#123;</span><br><span class="line">                <span class="keyword">struct</span> <span class="title class_">Hero</span> temp = heroArray[j];</span><br><span class="line">                heroArray[j] = heroArray[j + <span class="number">1</span>];</span><br><span class="line">                heroArray[j + <span class="number">1</span>] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//打印结构体信息</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printHero</span><span class="params">(<span class="keyword">struct</span> Hero heroArray[], <span class="type">int</span> len)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        cout &lt;&lt; heroArray[i].name &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; heroArray[i].age &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; heroArray[i].sex &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//2.创建数组存放5名英雄</span></span><br><span class="line">    Hero heroArray[<span class="number">5</span>] = &#123;</span><br><span class="line">            &#123;<span class="string">&quot;刘备&quot;</span>, <span class="number">23</span>, <span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;关于&quot;</span>, <span class="number">22</span>, <span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;张飞&quot;</span>, <span class="number">20</span>, <span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;赵云&quot;</span>, <span class="number">21</span>, <span class="string">&quot;男&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;貂蝉&quot;</span>, <span class="number">19</span>, <span class="string">&quot;女&quot;</span>&#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//3.对数组进行排序，按照年龄进行升序排序</span></span><br><span class="line">    <span class="type">int</span> len = <span class="built_in">sizeof</span>(heroArray) / <span class="built_in">sizeof</span>(heroArray[<span class="number">0</span>]);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;排序前打印：&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        cout &lt;&lt; heroArray[i].name &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; heroArray[i].age &lt;&lt; <span class="string">&quot;\t&quot;</span> &lt;&lt; heroArray[i].sex &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">paixu_Persion</span>(heroArray, len);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//4.将排序结果打印输出</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;排序后打印：&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">printHero</span>(heroArray, len);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io">扣小妖</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io/posts/4934793.html">http://liukuaizhuan.gitee.io/posts/4934793.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本站所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/C-%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8/">C++基础入门</a></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/fb32dd64e26579054d2252368f88c81c.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://lib.baomitu.com/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" alt="微信打赏"/></a><div class="post-qr-code-desc">微信打赏</div></li><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" alt="支付宝打赏"/></a><div class="post-qr-code-desc">支付宝打赏</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/b056b485.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">009.C++基础实战-通讯录管理系统</div></div></a></div><div class="next-post pull-right"><a href="/posts/f81b9c9e.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/27666aee9352884246f94ef7968abc3d.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">007.C++基础入门 - 指针</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/7162ef50.html" title="001.C++基础入门 - 初识C++"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/12323e005db5dc9b5c8194068fd91d1a.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-19</div><div class="title">001.C++基础入门 - 初识C++</div></div></a></div><div><a href="/posts/2cbb2ed2.html" title="002.C++基础入门 - 数据类型"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/9aae8c98625029bc6c9282b094623bbb.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-21</div><div class="title">002.C++基础入门 - 数据类型</div></div></a></div><div><a href="/posts/98ee46c0.html" title="003.C++基础入门 - 运算符"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/bc472ee28d45696821979067987e3ba7.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-24</div><div class="title">003.C++基础入门 - 运算符</div></div></a></div><div><a href="/posts/2e5085fc.html" title="004.C++基础入门 - 程序流程结构"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/d054658798b285e87606dc03ec1477bb.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-25</div><div class="title">004.C++基础入门 - 程序流程结构</div></div></a></div><div><a href="/posts/7bf3cba7.html" title="006.C++基础入门 - 函数"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/f3b717ca83ec282f1d66f831674af82a.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-28</div><div class="title">006.C++基础入门 - 函数</div></div></a></div><div><a href="/posts/66429bb0.html" title="005.C++基础入门 - 数组"><img class="cover" src="https://p.ananas.chaoxing.com/star3/origin/4a4262f048f7acac11e3b6ba064dca92.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-27</div><div class="title">005.C++基础入门 - 数组</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">扣小妖</div><div class="author-info__description">Email：  liukuaizhuan@qq.com</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">结构体基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9A%E4%B9%89%E5%92%8C%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">结构体定义和使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E6%95%B0%E7%BB%84"><span class="toc-number">3.</span> <span class="toc-text">结构体数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88"><span class="toc-number">4.</span> <span class="toc-text">结构体指针</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E5%B5%8C%E5%A5%97%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">5.</span> <span class="toc-text">结构体嵌套结构体</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E5%81%9A%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0"><span class="toc-number">6.</span> <span class="toc-text">结构体做函数参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%ADconst%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">7.</span> <span class="toc-text">结构体中const使用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E6%A1%88%E4%BE%8B1"><span class="toc-number">8.</span> <span class="toc-text">结构体案例1</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93%E6%A1%88%E4%BE%8B2"><span class="toc-number">9.</span> <span class="toc-text">结构体案例2</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象"><img src="https://p.ananas.chaoxing.com/star3/origin/7ea674ffcd12ef7d943d54c93e9203a4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="013.c++核心编程-类和对象"/></a><div class="content"><a class="title" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象">013.c++核心编程-类和对象</a><time datetime="2023-02-13T14:55:03.000Z" title="发表于 2023-02-13 22:55:03">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高"><img src="https://p.ananas.chaoxing.com/star3/origin/b6c49bb4892ad191edbc0363caf45530.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="012.C++核心编程-函数提高"/></a><div class="content"><a class="title" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高">012.C++核心编程-函数提高</a><time datetime="2023-02-13T14:51:24.000Z" title="发表于 2023-02-13 22:51:24">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/bff12c11.html" title="011.C++核心编程-引用"><img src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="011.C++核心编程-引用"/></a><div class="content"><a class="title" href="/posts/bff12c11.html" title="011.C++核心编程-引用">011.C++核心编程-引用</a><time datetime="2023-02-13T14:47:34.000Z" title="发表于 2023-02-13 22:47:34">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型"><img src="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="010.C++核心编程-内存分区模型"/></a><div class="content"><a class="title" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型">010.C++核心编程-内存分区模型</a><time datetime="2023-02-06T15:28:05.000Z" title="发表于 2023-02-06 23:28:05">2023-02-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统"><img src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="009.C++基础实战-通讯录管理系统"/></a><div class="content"><a class="title" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统">009.C++基础实战-通讯录管理系统</a><time datetime="2023-02-04T10:02:31.000Z" title="发表于 2023-02-04 18:02:31">2023-02-04</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 扣小妖</div><div class="footer_custom_text"><p style="margin:10px 0 5px"><a target="_blank" href="https://hexo.io/" style="margin-inline:5px">博客 hexo</a><a target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px">主题 Butterfly</a><a target="_blank" href="https://gitee.com/" style="margin-inline:5px">托管 Gitee</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://gcore.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://gcore.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>